/*
 * @Author: EltonLi lijiaming@uavi-tech.com
 * @Date: 2024-04-01 17:48:51
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-18 16:55:53
 * @FilePath: \PREMIUM_RTThread_V1.0\ZINO\hardware\led\led.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <rtthread.h>
#include "pin.h"
#include "drv_common.h"
#include "zino.h"

void zinoPwrLedOn(uint8_t led)
{
    switch (led)
    {
    case 0:
        rt_pin_mode(GET_PIN(B, 3), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 4), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 5), PIN_MODE_INPUT);
        break;
    case 1:
        rt_pin_mode(GET_PIN(B, 3), PIN_MODE_OUTPUT);
        rt_pin_mode(GET_PIN(B, 4), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 5), PIN_MODE_OUTPUT);

        rt_pin_write(GET_PIN(B, 3), PIN_HIGH);
        rt_pin_write(GET_PIN(B, 5), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 3), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 5), PIN_HIGH);
        break;
    case 2:
        rt_pin_mode(GET_PIN(B, 3), PIN_MODE_OUTPUT);
        rt_pin_mode(GET_PIN(B, 4), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 5), PIN_MODE_OUTPUT);

        rt_pin_write(GET_PIN(B, 5), PIN_HIGH);
        rt_pin_write(GET_PIN(B, 3), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 5), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 3), PIN_HIGH);
        break;
    case 3:
        rt_pin_mode(GET_PIN(B, 3), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 4), PIN_MODE_OUTPUT);
        rt_pin_mode(GET_PIN(B, 5), PIN_MODE_OUTPUT);

        rt_pin_write(GET_PIN(B, 4), PIN_HIGH);
        rt_pin_write(GET_PIN(B, 5), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 4), PIN_LOW);
        // rt_pin_write(GET_PIN(B, 5), PIN_HIGH);
        break;
    case 4:
        rt_pin_mode(GET_PIN(B, 3), PIN_MODE_INPUT);
        rt_pin_mode(GET_PIN(B, 4), PIN_MODE_OUTPUT);
        rt_pin_mode(GET_PIN(B, 5), PIN_MODE_OUTPUT);

        // rt_pin_write(GET_PIN(B, 4), PIN_HIGH);
        // rt_pin_write(GET_PIN(B, 5), PIN_LOW);
        rt_pin_write(GET_PIN(B, 4), PIN_LOW);
        rt_pin_write(GET_PIN(B, 5), PIN_HIGH);
        break;
    default:
        break;
    }
}

void pwrLedHandle(void)
{
    static uint8_t ledCnt;
    static uint16_t cycleCnt;
    static uint8_t ledCntMax;
    static uint8_t ledSet;
    static bool lowVoltageWarningFlag = 0;
    uint16_t blinkTime;
    static uint32_t blinkT;
    // static uint32_t vbatFullT;
    // usartPrintf("5V:%dmV\n",get_BatteryVoltage());
    // int16_t vbat = get_vbat_voltage_mv();
    // int16_t v5 = get_v5_voltage_mv();
    int16_t v5 = 5000;
    int16_t vbat = get_vbat_voltage_mv();

    /**计算要点亮多少个LED:
     * 0~3.3V 1个,
     * 3.3~3.6V 2个,
     * 3.6~3.9V 3个,
     * 3.9~4.2V 4个
     * */

    ledSet = (vbat > 3300) ? ((vbat - 3300) / 225 + 1) : 1;
    if (ledSet > 4)
        ledSet = 4;
    if (vbat < 3300)
    {
        /**低电压，快闪 */
        blinkTime = 250;
        lowVoltageWarningFlag = 1;
    }
    else
    {
        /**充电时，慢闪 */
        blinkTime = 500;
        lowVoltageWarningFlag = 0;
    }
    if (++cycleCnt > 15) /**LED 轮循周期，越大频闪越严重*/
    {
        cycleCnt = 0;
        ledCnt++;
        if (ledCnt > ledCntMax)
        {
            ledCnt = 0;
        }
    }
    if (rt_pin_read(GET_PIN(C, 13)) || lowVoltageWarningFlag)
    {
        /**最后一个LED闪烁 */
        if (millis() > blinkT + blinkTime)
        {
            ledCntMax = (ledSet > 0) ? (ledSet - 1) : 0;
        }
        else
        {
            ledCntMax = ledSet;
        }
        if (millis() > blinkT + blinkTime * 2)
        {
            blinkT = millis();
        }
    }
    else
    {
        if (v5 < 4900) // 5V升压失败时
        {
            if (millis() > blinkT + blinkTime / 2)
            {
                blinkT = millis();
                if (ledCntMax-- <= 0)
                {
                    ledCntMax = ledSet;
                }
            }
        }
        else
        {
            ledCntMax = ledSet;
        }
    }
    zinoPwrLedOn(ledCnt); /**LED轮循点亮 */
}
